﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.FSharp.Core;

namespace Cephei.Core
{
    public class CellFactory : ICell_Factory 
    {
        private static Lazy<ICell_Factory> _fac = new Lazy<ICell_Factory> (() =>
        {
            Assembly a = Assembly.GetExecutingAssembly ();
            string path = a.CodeBase.Substring (0, a.CodeBase.LastIndexOf ('/'));

            Assembly cellAssembly = Assembly.LoadFrom (path + "/Cephei.Kernel.dll");
            Type FactoryType = cellAssembly.GetType ("Cephei.Kernel.Cell_Factory");
            return (ICell_Factory) Activator.CreateInstance (FactoryType);
        });

        public static ICell_Factory Instance
        {
            get
            {
                return _fac.Value;
            }
        }

        public Generic.ICell<T> Create<T> ()
        {
            return _fac.Value.Create<T> ();
        }

        public Generic.ICell<T> Create<T> (T t)
        {
            return _fac.Value.Create<T> (t);
        }

        public Generic.ICell<T> Create<T> (IModel model, string mnmonic)
        {
            return _fac.Value.Create<T> (model, mnmonic);
        }

        public Generic.ICell<T> Create<T> (T t, IModel model, string mnmonic)
        {
            return _fac.Value.Create<T> (t, model, mnmonic);
        }

        public ICell Create (IModel model, string mnmonic)
        {
            return _fac.Value.Create (model, mnmonic);
        }

        public Generic.ICoCell<T> CreateFunc<T> (FSharpFunc<Unit, T> fastfunc)
        {
            return _fac.Value.CreateFunc<T> (fastfunc);
        }

        public Generic.ICoCell<T> CreateFunc<T> (FSharpFunc<Unit, T> fastfunc, IModel model, string mnmonic)
        {
            return _fac.Value.CreateFunc<T> (fastfunc, model, mnmonic);
        }
    }
/*
    /// <summary>
    /// Extension methods for cells.. to call factory functions
    /// </summary>
    public static class ICellExtentions
    {
    }
*/
}
